package com.weasel.im.common.codec;

import com.weasel.helper.JsonHelper;
import com.weasel.im.common.domain.Message;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.nio.charset.Charset;
import java.util.List;

/**
 * Created by Dylan on 2015/11/27.
 */
public class MessageDecoder extends ByteToMessageDecoder {


    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {

        String jsonMessge = byteBuf.toString(Charset.defaultCharset());
        Message message = JsonHelper.fromJsonString(jsonMessge,Message.class);
        list.add(message);
    }
}
